﻿Imports VN.INS.BL.BC
Imports System.Data
Imports Microsoft.Office.Interop
Imports System.IO

Partial Class Devida_MantenimientoPoa
    Inherits System.Web.UI.Page

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        If (Not (Page.IsPostBack)) Then
            'Carga de mantenimiento POA
            If Not (Session("GrillaPOA") Is Nothing) Then
                ViewState("POA") = Session("GrillaPOA").Substring(4)
            End If
            ViewState("VSg_num") = 0
            ViewState("VSusuario") = Session("user_id").ToString

            ListaPPER() 'SFB: Carga la lista de PPERs

            ListaEjecutor(ddlPPER.SelectedValue.ToString) 'SFB: Carga la lista de Ejecutores


            btn_reprogramar.Visible = False
            btn_reformular.Visible = False
            btnHistorial.Visible = False
            ibCodigos.Visible = False

            If Session("PERFIL") = "MO" Or Session("PERFIL") = "EP" Then

                'SFB: Ocultando botones y otros ...
                btn_reprogramar.Visible = False
                btn_reformular.Visible = False
                btnHistorial.Visible = False
                ibnuevoPoa.Visible = False
                ibCodigos.Visible = False

            End If

            'CargarGrilla() 'SFB: Carga de grilla de POAs

            CargarGrilla("", ddl_anhio.SelectedValue.ToString)

            Lista_anhio() 'SFB: Carga la lista de años

            'ddl_anhio.SelectedValue = "all"

            If Not (ViewState("POA") Is Nothing) Then
                CargarGrilla("", ddl_anhio.SelectedValue.ToString)
                Session("GrillaPOA") = Nothing
                If gv_impresion.Rows.Count > 0 Then
                    'gv_impresion.SelectRow(0)
                End If
            End If
        End If
        btn_reformular.Attributes.Add("onClick", "return confirm('Está seguro que desea Reformular?. Al hacer click en Aceptar');")
    End Sub

    'SFB: Este método carga el DropDownList PPER
    Public Sub ListaPPER()
        Dim objPPER As PPERBC
        Dim objData As DataTable
        objPPER = New PPERBC
        objData = New DataTable
        objData = objPPER.ListaPPER("")

        Dim drusuariorow As DataRow = objData.NewRow()
        drusuariorow("ABREVIATURA") = "--Todos--"
        drusuariorow("PPER") = ""

        'SFB: Añadiendo la fila al objData
        objData.Rows.Add(drusuariorow)

        'SFB: Asociando datos al DropDownList PPER
        ddlPPER.DataSource = objData
        ddlPPER.DataTextField = "ABREVIATURA"
        ddlPPER.DataValueField = "PPER"
        ddlPPER.DataBind()
        ddlPPER.SelectedValue = ""
    End Sub

    'SFB: Este método carga el DropDownList EJECUTOR
    Public Sub ListaEjecutor(ByVal strPPER As String)
        Dim objReporte As ReporteBC
        Dim objData As DataTable

        objReporte = New ReporteBC
        objData = New DataTable
        objData = objReporte.ListaEjecutor(strPPER, "", "")

        Dim drusuariorow As DataRow = objData.NewRow()
        drusuariorow("DESCRIPCION") = "--Todos--"
        drusuariorow("EJECUTOR") = ""

        objData.Rows.Add(drusuariorow)

        ddlEjecutor.DataSource = objData
        ddlEjecutor.DataTextField = "DESCRIPCION"
        ddlEjecutor.DataValueField = "EJECUTOR"
        ddlEjecutor.DataBind()
        ddlEjecutor.SelectedValue = ""
    End Sub

    'Carga de grilla POA (Por defecto)
    Public Sub CargarGrilla()

        Dim objMantenimientoPoaBC As MantenimientoPoaBC
        objMantenimientoPoaBC = New MantenimientoPoaBC

        Dim objData As DataTable

        If Session("Perfil") = "AD" Then
            objData = objMantenimientoPoaBC.ListaPOA("")
        Else
            objData = objMantenimientoPoaBC.ListaPOA(Session("Persona"))
        End If

        gv_impresion.DataSource = objData
        gv_impresion.DataBind()
        'gv_impresion.SelectedIndex = -1
    End Sub

    'Carga de grilla poa con filtro
    Public Sub CargarGrilla(ByVal strNombre As String, ByVal stranhio As String)
        Dim objMantenimientoPoaBC As MantenimientoPoaBC
        Dim objData As DataTable
        objMantenimientoPoaBC = New MantenimientoPoaBC

        If ViewState("POA") Is Nothing Then
            ViewState("POA") = txtPOA.Text
        End If

        'Llenado de ceros a la izquierda
        Dim strCodigo As String = ViewState("POA")
        Dim s As String = ""
        If strCodigo.ToString.Length < 4 And strCodigo.Length > 0 Then
            For i As Integer = 1 To 4 - strCodigo.ToString.Length
                s += "0"
            Next
            s += strCodigo.ToString
            strCodigo = "POA-" + s
        Else
            strCodigo = "POA-" + ViewState("POA")
        End If
        If strCodigo <> "0000" Then
            txtPOA.Text = strCodigo.Substring(4)
        End If

        If Session("Perfil") = "AD" Then
            objData = objMantenimientoPoaBC.ListaPOA(strNombre, ddlPPER.SelectedValue.ToString, ddlEjecutor.SelectedValue.ToString, strCodigo, stranhio, "")
        Else
            objData = objMantenimientoPoaBC.ListaPOA(strNombre, ddlPPER.SelectedValue.ToString, ddlEjecutor.SelectedValue.ToString, strCodigo, stranhio, Session("Persona"))
        End If

        gv_impresion.DataSource = objData
        gv_impresion.DataBind()
        'gv_impresion.SelectedIndex = -1

    End Sub

    Protected Sub gv_impresion_RowDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewRowEventArgs) Handles gv_impresion.RowDataBound
        If e.Row.RowType = DataControlRowType.DataRow Then
            ' ASIGNA EVENTOS
            e.Row.Attributes.Add("OnMouseOver", "Resaltar_On(this);")
            e.Row.Attributes.Add("OnMouseOut", "Resaltar_Off(this);")
            e.Row.Attributes("OnClick") = Page.ClientScript.GetPostBackClientHyperlink(Me.gv_impresion, "Select$" + e.Row.RowIndex.ToString)
            ' ESTADO
            If e.Row.Cells(7).Text = "A" Then
                e.Row.Cells(7).Text = "ACTIVO"
            ElseIf e.Row.Cells(7).Text = "I" Then
                e.Row.Cells(7).Text = "INACTIVO"
                e.Row.Cells(7).ForeColor = Drawing.Color.Red
            ElseIf e.Row.Cells(7).Text = "C" Then
                e.Row.Cells(7).Text = "CONCLUIDO"
                e.Row.Cells(7).BackColor = Drawing.Color.LightSkyBlue

            ElseIf e.Row.Cells(7).Text = "N" Then
                e.Row.Cells(7).Text = "ANULADO"
                e.Row.Cells(7).ForeColor = Drawing.Color.LightSkyBlue
            End If

            'Esto es para el campo Año Firma 
            Select Case e.Row.Cells(2).Text
                Case "2012"
                    e.Row.Cells(2).BackColor = Drawing.Color.LightGray
                Case "2013"
                    e.Row.Cells(2).BackColor = Drawing.Color.DarkTurquoise
                    e.Row.Cells(2).ForeColor = Drawing.Color.Black
                Case "2014"
                    e.Row.Cells(2).BackColor = Drawing.Color.MediumPurple
                    e.Row.Cells(2).ForeColor = Drawing.Color.White
                Case Else
                    e.Row.Cells(2).BackColor = Drawing.Color.LightGray
            End Select

            'Esto es para el estado de reprogramación/ reformulación del POA
            If e.Row.Cells(8).Text = "PROCESO REPROG." Then
                e.Row.Cells(8).Text = "PROCESO INGRESO POA"
                e.Row.Cells(8).BackColor = Drawing.Color.Black
                e.Row.Cells(8).ForeColor = Drawing.Color.Orange
            End If

        End If

    End Sub
  
    Protected Sub gv_impresion_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles gv_impresion.SelectedIndexChanged
        'captura de parametros de fila seleccionada
        Dim hfPOA As HiddenField = CType(gv_impresion.SelectedRow.FindControl("POA"), HiddenField)
        Dim hfVersion As HiddenField = CType(gv_impresion.SelectedRow.FindControl("hfVersion"), HiddenField)
        Dim hfEstadoReprog As HiddenField = CType(gv_impresion.SelectedRow.FindControl("EstadoReprog"), HiddenField)

        Try
            If Not hfPOA Is Nothing Then
                Session("GrillaPOA") = hfPOA.Value.ToString
                ViewState("POA") = Session("GrillaPOA").ToString.Substring(4)
                ViewState("Version") = hfVersion.Value.ToString
                Session("EstadoReprog") = hfEstadoReprog.Value.ToString 'Contiene el estado de Reprogramación/ Reformulación como variable tipo session ósea accesible desde toda la aplicación

            End If          
        Catch ex As Exception
            Throw ex
        End Try
    End Sub
   
    Protected Sub ibnuevoPoa_Click(ByVal sender As Object, ByVal e As System.Web.UI.ImageClickEventArgs) Handles ibnuevoPoa.Click
        'redireccionamos a detalle de mantenimiento nuevo
        ViewState("Accion1") = "N"
        ViewState("Accion") = ViewState("Accion1")
        Response.Redirect("MantenimientoPoaPestana.aspx?Accion=" & ViewState("Accion"), False)
    End Sub

    Protected Sub btnHistorial_Click(ByVal sender As Object, ByVal e As System.Web.UI.ImageClickEventArgs) Handles btnHistorial.Click
        'redireccionamos a historicos de poa seleccionado
        If gv_impresion.SelectedIndex = -1 Then
            alerta.Mostrar_Alerta("Error", "Debe seleccionar un Registro")
        Else
            Response.Redirect("Poa_historico.aspx?Param=" + Session("GrillaPOA").ToString, False)
        End If
    End Sub

    Protected Sub btn_modificar_Click(ByVal sender As Object, ByVal e As System.Web.UI.ImageClickEventArgs) Handles btn_modificar.Click
        'redireccionamos a detalle de mantenimiento edicion de poa seleccionado
        If gv_impresion.SelectedIndex = -1 Then
            alerta.Mostrar_Alerta("Error", "Debe seleccionar un Registro")
        Else
            ViewState("Accion1") = "M"
            ViewState("Accion") = ViewState("Accion1")

            Response.Redirect("MantenimientoPoaPestana.aspx?Param=" + Session("GrillaPOA").ToString & "&Accion=" & ViewState("Accion") & "&Version=" & ViewState("Version"), False)

        End If
    End Sub

    Protected Sub boton_busca_Click(ByVal sender As Object, ByVal e As System.Web.UI.ImageClickEventArgs) Handles boton_busca.Click
        ViewState("POA") = Nothing
        'carga grilla con filtro
        CargarGrilla(txt_NombrePoa.Text, ddl_anhio.SelectedValue.ToString)
    End Sub

    Protected Sub btn_reprogramar_Click(ByVal sender As Object, ByVal e As System.Web.UI.ImageClickEventArgs) Handles btn_reprogramar.Click
        'redireccionamos a reprogramar poa seleccionado
        If gv_impresion.SelectedIndex = -1 Then
            alerta.Mostrar_Alerta("Error", "Debe seleccionar un Registro")
        Else
            ViewState("RR") = "R1"
            Response.Redirect("ReprogramacionPoapestana.aspx?Param=" + Session("GrillaPOA").ToString & "&RR=" & ViewState("RR"), False)

        End If
    End Sub

    Protected Sub btn_reformular_Click(ByVal sender As Object, ByVal e As System.Web.UI.ImageClickEventArgs) Handles btn_reformular.Click
        'redireccionamos a reformular poa seleccionado
        If gv_impresion.SelectedIndex = -1 Then
            alerta.Mostrar_Alerta("Error", "Debe seleccionar un Registro")
        Else
            ViewState("RR") = "R2"
            Response.Redirect("ReprogramacionPoapestana.aspx?Param=" + Session("GrillaPOA").ToString & "&RR=" & ViewState("RR"), False)
        End If
    End Sub

    Protected Sub ibn_salir_Click(ByVal sender As Object, ByVal e As System.Web.UI.ImageClickEventArgs) Handles ibn_salir.Click
        'Salir
        Session("GrillaPOA") = Nothing
        Response.Redirect("Inicio.aspx", False)
    End Sub

    Public Sub Lista_anhio()
        Dim objlista_anhio As MantenimientoPeriodoBC
        Dim objdt As DataTable
        objlista_anhio = New MantenimientoPeriodoBC
        objdt = New DataTable
        objdt = objlista_anhio.ListaAño()

        Dim drlistaanhiorow As DataRow = objdt.NewRow()
        drlistaanhiorow("ANHIO") = "--Seleccione--"
        drlistaanhiorow("CODIGO") = "all"
        objdt.Rows.Add(drlistaanhiorow)

        ddl_anhio.DataSource = objdt
        ddl_anhio.DataTextField = "ANHIO"
        ddl_anhio.DataValueField = "CODIGO"
        ddl_anhio.DataBind()
        ddl_anhio.SelectedIndex = objdt.Rows.Count - 2

    End Sub

    Protected Sub ddlPPER_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles ddlPPER.SelectedIndexChanged
        ListaEjecutor(ddlPPER.SelectedValue.ToString)
    End Sub

    Protected Sub btnLimpiar_Click(ByVal sender As Object, ByVal e As System.Web.UI.ImageClickEventArgs) Handles btnLimpiar.Click
        ddlPPER.SelectedValue = ""
        ddlEjecutor.SelectedValue = ""
        txtPOA.Text = ""
        txt_NombrePoa.Text = ""
        ddl_anhio.SelectedValue = "all"
    End Sub

    Protected Sub ibCodigos_Click(ByVal sender As Object, ByVal e As System.Web.UI.ImageClickEventArgs) Handles ibCodigos.Click
        'redireccionamos a ingreso de codigos a poa seleccionado
        If gv_impresion.SelectedIndex = -1 Then
            alerta.Mostrar_Alerta("Error", "Debe seleccionar un Registro")
        Else
            Response.Redirect("DigitaCodigo.aspx?POA=" & ViewState("POA") & "&Version=" & ViewState("Version"), False)
        End If
    End Sub
    'cesar 2013  --> esta alerta sirve para mostrar el mensaje de error de visual y asi evitar las pantallas amarillas
    'alerta.Mostrar_Alerta("Error", ex.Message)

    Protected Sub ddl_anhio_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles ddl_anhio.SelectedIndexChanged
        Session("GrillaPOA") = Nothing
        txtPOA.Text = ""
        'Carga grilla con filtro
        'CargarGrilla(txt_NombrePoa.Text, ddl_anhio.SelectedValue.ToString)
        CargarGrilla(txt_NombrePoa.Text, ddl_anhio.SelectedValue.ToString)
    End Sub

End Class
